package com.action.admin.goods;

import java.util.Date;
import java.util.List;

import com.action.BaseAction;
import com.dao.GoodsTypeDao;
import com.entity.Employee;
import com.entity.GoodsType;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;

/**
 * 
 * @author Administrator
 * 
 */
//@ParentPackage("json-default")
@ParentPackage("extendsjson-default")
@Namespace("/goods")
@ResultPath("/WEB-INF/pages")
public class GoodsTypeAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(GoodsTypeAction.class);

	/**
	 * 进入商品类型添加
	 * 
	 * @return
	 */
	@Action(value = "viewgtype", results = {
			@Result(name = "success", location = "goodsTypeAdd.jsp")
//			@Result(name = "login", location = "../user/index.do", type = "redirect") 
			})
	public String addgtype() {
		Employee emp = (Employee) session("userinfo");
		if (emp == null)
			return LOGIN;
		return SUCCESS;
	}
	
	/**
	 * 增加功能
	 * 
	 * @return
	 */
	@Action(value = "addgoodstype", results = { 
			@Result(name = "error", location = "goodsTypeAdd.jsp"),
			@Result(name = "success", location = "ListGoodsTypes.do", type = "redirect") })
	public String addGoodsTypes() {
		boolean flag = false;
		flag = goodsTypeDao.saveGoodsType(goodsType);
		if (flag) {
			return SUCCESS;
		} else {
			return ERROR;
		}
	}

	/**
	 * 修改
	 * 
	 * @return
	 */
	@Action(value = "updategtype", results = { 
			@Result(name = "error", location = "goodsTypeAdd.jsp"),
			@Result(name = "success", location = "ListGoodsTypes.do", type = "redirect") })
	public String updateGoodsTypes() {
		boolean flag = false;
		flag = goodsTypeDao.saveGoodsType(goodsType);
		if (flag) {
			return SUCCESS;
		} else {
			return ERROR;
		}
	}
	
	/**
	 * 商品类型详细信息
	 * 
	 * @return
	 */
	@Action(value = "editgoodstype", results = { 
			@Result(name = "success", location = "goodsTypeAdd.jsp"),
			@Result(name = "error", location = "ListGoodsTypes.do", type = "redirect")})
	public String editgoodstype() {
		try{
		goodsType = goodsTypeDao.editGoodsType(id);
		}catch(Exception e){
			e.printStackTrace();
			logger.error(e.getMessage());
			return 	ERROR;
		}
		return SUCCESS;
		
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	@Action(value = "delgtypes", results = { 
			@Result(name = "success", type = "json"), 
			@Result(name = "error", location = "goodsTypeAdd.jsp") })
	public String delGoodsTypes() {
		String flag2 = "1";
		try{
			this.setFlag(goodsTypeDao.delGoodsTypes(id));
			if(flag){
				flag2 = "0";
			}
		}catch(Exception e){
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		this.outJsonString(flag2);
		return SUCCESS;
	}

	/**
	 * 列表
	 * 
	 * @return
	 */
	@Action(value = "ListGoodsTypes", results = { 
			@Result(name = "success", location = "goodsTypeList.jsp"),
			@Result(name = "error", location = "goodsTypeList.jsp") })
	public String listGoodsTypes() {
		try {
			list = goodsTypeDao.listGoodsType();
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			return ERROR;
		}
		return SUCCESS;
	}

	
	private GoodsTypeDao goodsTypeDao = (GoodsTypeDao) ioc.newInstance(GoodsTypeDao.class);
	private GoodsType goodsType;
	
	private List list;
	private String id;
	boolean flag = false;
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public GoodsType getGoodsType() {
		return goodsType;
	}

	public void setGoodsType(GoodsType goodsType) {
		this.goodsType = goodsType;
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}
	
	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}


}
